package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.AjaxResult;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.model.entity.MdlModelType;
import com.guodi.model.service.IMdlModelTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * @描述：模型体系表控制类
 * @作者: 代鹏刚
 * @日期: 2020-06-17
 */
@RestController
@RequestMapping("/MdlModelType")
@Api(value = "API - MdlModelTypeController", description = "模型体系表")
public class MdlModelTypeController extends BaseController {

    @Resource
    public IMdlModelTypeService mdlModelTypeService;

    /**
    * @描述: 获取模型体系列表数据
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020-06-17
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 获取模型体系列表数据")
    public AjaxResult listByEntity(){
        LambdaQueryWrapper<MdlModelType> wrapper = new QueryWrapper().lambda();
        wrapper.orderByAsc(MdlModelType::getSort);
        List<MdlModelType> mdlModelTypes = this.mdlModelTypeService.list(wrapper);
        return new AjaxResult(mdlModelTypes);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult findById(String id) {
        if(StringUtils.isBlank(id)){
            return new AjaxResult(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MdlModelType mdlModelType = mdlModelTypeService.getById(id);
        return new AjaxResult(mdlModelType);
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态(1.启用, 0:禁用)", required = false, dataType = "int", paramType = "query")
    })
    public AjaxResult save(@ApiIgnore MdlModelType mdlModelType){
        if(mdlModelType.getStatus() == null){
            mdlModelType.setStatus(1);
        }
        if (StringUtils.isNotBlank(mdlModelType.getCode())) {
            HashMap hashMap = new HashMap();
            hashMap.put("code",mdlModelType.getCode());
            //验证修改的code唯一
            List<MdlModelType> list = (List<MdlModelType>) mdlModelTypeService.listByMap(hashMap);
            if (list.size() > 0 && !list.get(0).getId().equals(mdlModelType.getId())) {
                return new AjaxResult(StatusCode.ERROR_CODE_40005, "该编码已经存在,请更换！");
            }
        }
        prepareData(mdlModelType, true);
        mdlModelType.setId(UuidUtil.get32UUID());
        String deptId = this.getUser().getDeptId();
        if(StringUtils.isNotBlank(deptId)){
            mdlModelType.setOrgId(deptId);
        }
        mdlModelTypeService.save(mdlModelType);
        return new AjaxResult();
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序", dataType = "int", paramType = "query"),
             @ApiImplicitParam(name = "status", value = "状态(1.启用, 0:禁用)", dataType = "int", paramType = "query")
    })
    public AjaxResult update(@ApiIgnore MdlModelType mdlModelType) {
        if (StringUtils.isNotBlank(mdlModelType.getCode())) {
            HashMap hashMap = new HashMap();
            hashMap.put("code",mdlModelType.getCode());
            //验证修改的code唯一
            List<MdlModelType> list = (List<MdlModelType>) mdlModelTypeService.listByMap(hashMap);
            if (list.size() > 0 && !list.get(0).getId().equals(mdlModelType.getId())) {
                return new AjaxResult(StatusCode.ERROR_CODE_40005, "该编码已经存在,请更换！");
            }
        }
        prepareData(mdlModelType, false);
        mdlModelTypeService.updateById(mdlModelType);
        return new AjaxResult();
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-06-17
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String id){
        mdlModelTypeService.removeById(id);
        return new AjaxResult();
    }


    private void prepareData(MdlModelType mdlModelType, boolean isSave) {
        if(isSave){
            mdlModelType.setCreator(this.getUser().getUserId().toString());
            mdlModelType.setCreateTime(new Date());
            //防止误修改
            mdlModelType.setModifier(null);
            mdlModelType.setModifyTime(null);
        }else{
            mdlModelType.setModifier(this.getUser().getUserId().toString());
            mdlModelType.setModifyTime(new Date());
            //防止误修改
            mdlModelType.setCreator(null);
            mdlModelType.setCreateTime(null);
        }
    }
}
